package com.dingdang.controller.account_management;


import com.dingdang.data.Result;
import com.dingdang.em.ResultEnum;
import com.dingdang.entity.User;
import com.dingdang.mapper.UserMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("userController")
@Api("用户管理接口")
public class UserController
{
    @Autowired
    private UserMapper userMapper;

    //搜索
    @GetMapping
    @ApiOperation("用户搜索")
    @ApiImplicitParam(name = "void",value = "空",required = true,paramType = "query")
    public Result selectUser()
    {
        User User = new User();
        List<User> userList = userMapper.selectList(null);
        return Result.response(ResultEnum.SUCCESS).setData(userList);
    }

    //用户增加
    @PostMapping
    @ApiOperation("用户增加")
    @ApiImplicitParam(name = "User",value = "用户实体类",required = true,paramType = "body")
    public Result insertUser(@RequestBody User User)
    {
        int i = userMapper.insert(User);
        if (i==1)
        {
            return Result.response(ResultEnum.SUCCESS);
        }
        else
        {
            return Result.response(ResultEnum.ERROR);
        }
    }

    //用户回显
    @PostMapping("/userToUpdate")
    @ApiOperation("用户回显")
    @ApiImplicitParam(name = "id",value = "用户id",required = true,paramType = "query")
    public Result ToUpdateUser(@RequestParam("id") Long id)
    {
        User User = new User();
        User = userMapper.selectById(id);
        return Result.response(ResultEnum.SUCCESS).setData(User);
    }

    //用户修改
    @PutMapping
    @ApiOperation("用户修改")
    @ApiImplicitParam(name = "User",value = "用户实体类",required = true,paramType = "body")
    public Result UpdateUser(@RequestBody User user)
    {
        int i = userMapper.updateById(user);
        if (i==1)
        {
            return Result.response(ResultEnum.SUCCESS);
        }
        else
        {
            return Result.response(ResultEnum.ERROR);
        }
    }

    @DeleteMapping()
    @ApiOperation("用户删除")
    @ApiImplicitParam(name = "id",value = "用户id",required = true,paramType = "query")
    public Result deleteInfo(@RequestParam(value = "id") Long []ids)
    {
        List<Long> deleteBatch = new ArrayList<>();
        for (Long addr:ids)
        {
            deleteBatch.add(addr);
        }
        int i = userMapper.deleteBatchIds(deleteBatch);
        if (i==1)
        {
            return Result.response(ResultEnum.SUCCESS);
        }
        else
        {
            return Result.response(ResultEnum.ERROR);
        }
    }
}
